//
//  LZVideoCellPresenter.m
//  laziz_Consumer
//
//  Created by xbn_iMac on 2016/11/17.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZVideoCellPresenter.h"
#import "LZVideoReviewListViewController.h"
#import "LZVideoDetailViewController.h"
#import "LZVideoListModel.h"
#import "LZUserOperationPresenter.h"
#import "LZUserModel.h"
#import "LZLoginViewController.h"
#import "LZCheckCouponAndGrouponPresenter.h"
#import "LZZanAndCollectStatusPresenter.h"
#import "LAWriteReviewViewController.h"
#import "LZPromotionStatusModel.h"
#import "LZAlertView.h"
#import "TShareView.h"
#import "TShareEventHandler.h"
#import "LZVideoPresenter.h"
#import "extobjc.h"

#import "LZVideoCellPresenterForCoupon.h"
#import "LZVideoCellPresenterForGroupon.h"

@interface LZVideoCellPresenter ()
@property (nonatomic, strong) TShareView * shareView;

@property (nonatomic, copy) NSString *selectedIndexPathIdentifier;

@property (nonatomic, strong) LZVideoCellPresenterForCoupon *presenterForCoupon;

@property (nonatomic, strong) LZVideoCellPresenterForGroupon *presenterForGroupon;

@end

@implementation LZVideoCellPresenter

/**
 *  @author Zhoudan, 16-10-26 10:10:23
 *
 *  协议方法 btnIndex: 0点赞 1收藏  2评价  3分享   4更多  5抢券详情   6拼团详情  7右上角写评论按钮   8右上角收藏按钮 
 */
- (void)dealClickCellButtonIndex:(VideoCellDelegateClickEventCode)index andCellIndexPath:(NSIndexPath *)cellIndexPath andNavigationController:(UINavigationController*)navigationController andHidesBottomBarWhenPushed:(BOOL)hidesBottomBarWhenPushed andCellModel:(id)model andOtherObject:(id)object andRefreshBlcok:(void(^)(VideoCellRefreshType, BOOL))refreshBlcok {

    LZVideoListModel *videoModel = (LZVideoListModel*)model;
    
//    WS(weakSelf)
    
    switch (index) {
        case kVideoCellDelegate_zanEvent: // 点赞
        {
            DLog(@"点赞");
            if ([self checkLoginStateWithNavigationController:navigationController andHidesBottomBarWhenPushed:hidesBottomBarWhenPushed]) {
                // 如果已登录
                [LZUserOperationPresenter zanOrCancelZanVideoOrRestaurantWithSellerID:videoModel.sellerId PromotionID:videoModel.videoId Type:@"1" OperationType:@"1" ResultSuccess:^(NSInteger totleCount, NSInteger operationType) {
                    
                    videoModel.isLiked = operationType == 1 ? YES : NO;
                    videoModel.likesCount = totleCount;
                    
                    [[LZZanAndCollectStatusPresenter shareInstance] addZanVideoWithId:videoModel.videoId];
                    
                    if (refreshBlcok) {
                        refreshBlcok(VideoCellRefreshType_Zan ,YES);
                    }
                } ResultFailed:^(NSString *remind) {
                    if (refreshBlcok) {
                        refreshBlcok(VideoCellRefreshType_Zan ,NO);
                    }
                }];
            }
        }
            
            break;
        case kVideoCellDelegate_collectEvent: // 收藏
        {
            DLog(@"收藏");
            if ([self checkLoginStateWithNavigationController:navigationController andHidesBottomBarWhenPushed:hidesBottomBarWhenPushed]) {
                // 如果已登录
                // 操作类型（1收藏，0取消收藏）
                [LZUserOperationPresenter collectOrCancelCollectVideoOrRestaurantWithSellerID:videoModel.sellerId PromotionID:videoModel.videoId FavoriteType:@"1" OperationType:videoModel.isStore?@"0":@"1" ResultSuccess:^(NSInteger totleCount, NSInteger operationType) {
                    DLog(@"-==-=====成功");
                    videoModel.isStore = operationType == 1 ? YES : NO;
                    videoModel.storeCount = totleCount;
                    [[LZZanAndCollectStatusPresenter shareInstance] addCollectVideoWithId:videoModel.videoId andCollectStatus:videoModel.isStore];
                    
                    if (refreshBlcok) {
                        refreshBlcok(VideoCellRefreshType_Collect ,YES);
                    }
                    
                } ResultFailed:^(NSString *remind) {
                    DLog(@"%@",remind);
                    if (refreshBlcok) {
                        refreshBlcok(VideoCellRefreshType_Collect ,NO);
                    }
                }];
            }
        }
            
            break;
        case kVideoCellDelegate_commentEvent: // 评价
        {
            self.selectedIndexPathIdentifier = nil;
            
            DLog(@"评价");
            LZVideoReviewListViewController *vc = [[LZVideoReviewListViewController alloc] init];
            vc.videoId = [videoModel.videoId copy];
            vc.restaurantId = [videoModel.sellerId copy];
            if (hidesBottomBarWhenPushed) {
                vc.hidesBottomBarWhenPushed = YES;
            }
            [navigationController pushViewController:vc animated:YES];
            
            vc.refreshNumBlock = ^(NSInteger num) {
                videoModel.remarkCountPerson = num;
                if (refreshBlcok) {
                    refreshBlcok(VideoCellRefreshType_Review ,YES);
                }
            };
        }
            break;
        case kVideoCellDelegate_shareEvent: // 分享
        {
            DLog(@"分享");
            WS(weakSelf)
            [self.shareView shareViewOpen];
            self.shareView.shareViewButtonActionBlock = ^(TSHAREVIEW_BUTTON_TYPE type){
                [weakSelf shareAction:type videoModel:videoModel refreshBlock:refreshBlcok];
            };
        }
            break;
        case kVideoCellDelegate_goDetailEvent:   // 更多,查看视频详情
        {
            self.selectedIndexPathIdentifier = nil;
            
            LZVideoDetailViewController *vc = [[LZVideoDetailViewController alloc] init];
            vc.videoID = videoModel.videoId;
            vc.limitCountDownCalculator = object;
            //数据请求成功Block
            [vc setUpdateNumBlock:^(NSInteger zanCount, NSInteger collectCount, NSInteger msgCount, NSInteger shareCount) {
                videoModel.likesCount = zanCount;
                videoModel.storeCount = collectCount;
                videoModel.remarkCountPerson = msgCount;
                videoModel.shareCount = shareCount;
                
                if (refreshBlcok) {
                    refreshBlcok(VideoCellRefreshType_AllCount,YES);
                }
            }];
            
            //点赞Block
            [vc setOperationZanBlock:^(NSInteger totleCount, NSInteger operationType) {
                videoModel.isLiked = operationType == 1 ? YES : NO;
                videoModel.likesCount = totleCount;
                if (operationType == 1) {
                    [[LZZanAndCollectStatusPresenter shareInstance] addZanVideoWithId:videoModel.videoId];
                }
                if (refreshBlcok) {
                    refreshBlcok(VideoCellRefreshType_Zan ,YES);
                }

            }];
            
            //收藏Block
            [vc setOperationCollectBlock:^(NSInteger totleCount, NSInteger operationType) {

                videoModel.isStore = operationType == 1 ? YES : NO;
                videoModel.storeCount = totleCount;
                
                [[LZZanAndCollectStatusPresenter shareInstance] addCollectVideoWithId:videoModel.videoId andCollectStatus:videoModel.isStore];

                if (refreshBlcok) {
                    refreshBlcok(VideoCellRefreshType_Collect ,YES);
                }
            }];
            
            //分享Block
            [vc setOperationShareBlock:^(NSInteger totleCount) {
                videoModel.shareCount = totleCount;
                if (refreshBlcok) {
                    refreshBlcok(VideoCellRefreshType_Share ,YES);
                }
            }];
            
            if (hidesBottomBarWhenPushed) {
                vc.hidesBottomBarWhenPushed = YES;
            }
            
            [navigationController pushViewController:vc animated:YES];
        }
            break;
        case kVideoCellDelegate_goCouponEvent:   // 进入抢券详情
        {
            if (!_presenterForCoupon) {
                _presenterForCoupon = [[LZVideoCellPresenterForCoupon alloc] init];
            }
            
            [self.presenterForCoupon couponDealClickCellButtonIndex:index andCellIndexPath:cellIndexPath andNavigationController:navigationController andHidesBottomBarWhenPushed:hidesBottomBarWhenPushed andCellModel:model andOtherObject:object andRefreshBlcok:refreshBlcok];
        }
            break;
        case kVideoCellDelegate_goGroupEvent:   // 进入拼团详情
        {
            if (!_presenterForGroupon) {
                _presenterForGroupon = [[LZVideoCellPresenterForGroupon alloc] init];
            }
            
            [self.presenterForGroupon grouponDealClickCellButtonIndex:index andCellIndexPath:cellIndexPath andNavigationController:navigationController andHidesBottomBarWhenPushed:hidesBottomBarWhenPushed andCellModel:model andOtherObject:object andRefreshBlcok:refreshBlcok];
        }
            break;
        case kVideoCellDelegate_topRightCornerCommentEvent: // 右上角写评论按钮
        {
            DLog(@"右上角写评论按钮");
            self.selectedIndexPathIdentifier = nil;
            if ([self checkLoginStateWithNavigationController:navigationController andHidesBottomBarWhenPushed:hidesBottomBarWhenPushed]) {
                LAWriteReviewViewController *vc = [[LAWriteReviewViewController alloc] initWithNibName:@"LAWriteReviewViewController" bundle:nil] ;
                vc.type = WriteReviewType_Video;
                vc.sellerId = [videoModel.sellerId copy];
                vc.videoId = [videoModel.videoId copy];
                vc.video_noRemarkId = [videoModel.noRemarkId copy];
                [navigationController pushViewController:vc animated:YES];
                
                vc.uploadReviewBlock = ^(BOOL isUploadSuccess) {
                    if (refreshBlcok) {
                        refreshBlcok(VideoCellRefreshType_Remove ,YES);
                    }
                };
            }
        }
            break;
        case kVideoCellDelegate_topRightCornerCollectEvent: // 右上角收藏按钮
        {
            DLog(@"右上角收藏按钮");
            // 操作类型（1收藏，0取消收藏）
            [LZUserOperationPresenter collectOrCancelCollectVideoOrRestaurantWithSellerID:videoModel.sellerId PromotionID:videoModel.videoId FavoriteType:@"1" OperationType:videoModel.isStore?@"0":@"1" ResultSuccess:^(NSInteger totleCount, NSInteger operationType) {
                DLog(@"-==-=====成功");
                videoModel.isStore = operationType == 1 ? YES : NO;
                videoModel.storeCount = totleCount;
                [[LZZanAndCollectStatusPresenter shareInstance] addCollectVideoWithId:videoModel.videoId andCollectStatus:videoModel.isStore];
                
                if (refreshBlcok) {
                    refreshBlcok(VideoCellRefreshType_Collect ,YES);
                }
                
            } ResultFailed:^(NSString *remind) {
                DLog(@"%@",remind);
                if (refreshBlcok) {
                    refreshBlcok(VideoCellRefreshType_Collect ,NO);
                }
            }];

        }
            break;
            
        default:
            break;
    }
}

- (BOOL)checkLoginStateWithNavigationController:(UINavigationController*)navigationController andHidesBottomBarWhenPushed:(BOOL)hidesBottomBarWhenPushed {
    if ([[LZUserModel Instance] isLogin]) {
        return YES;
    }
    else {
        // 如果未登录
        LZLoginViewController * vc = [[LZLoginViewController alloc] init];
        if (hidesBottomBarWhenPushed) {
            vc.hidesBottomBarWhenPushed = YES;
        }
        [navigationController pushViewController:vc animated:YES];
        
        return NO;
    }
}

#pragma mark - LZAlerView

- (void)showAlertView:(NSString*)message {
    LZAlertView *alert = [[LZAlertView alloc] initWithContent:message andImage:nil cancelButtonTitle:nil otherButtonTitles:LZLocalizedString(@"confirm", nil),nil];
    [alert show];
}

#pragma mark - Share

- (void)shareAction:(NSInteger)type videoModel:(LZVideoListModel *)vModel refreshBlock:(void (^)(VideoCellRefreshType type,BOOL isRefresh))refreshBlock
{
    /* 分享视频 */
    SSDKPlatformType sharePlatformType = SSDKPlatformTypeUnknown;
    
    switch (type) {
        case 0:
            sharePlatformType = SSDKPlatformTypeSMS;
            break;
        case 1:
            sharePlatformType = SSDKPlatformTypeMail;
            break;
        case 2:
            sharePlatformType = SSDKPlatformTypeFacebook;
            break;
        case 3:
            sharePlatformType = SSDKPlatformTypeTwitter;
            break;
    }
    
    [TShareEventHandler shareWithShareURL:nil
                            shareImageUrl:@[[NSString stringWithFormat:@"%@%@",APIBaseImageUrl,vModel.videoPhoto]]
                               shareTitle:[NSString stringWithFormat:LZLocalizedString(@"share_video_title", nil),vModel.promotionTitle]
                                shareText:[NSString stringWithFormat:LZLocalizedString(@"share_video_content", nil),vModel.promotionTitle]
                                shareType:sharePlatformType
                                  topView:nil
                               stateBlock:^(SSDKResponseState state) {
                                   if (state == SSDKResponseStateSuccess) {
                                       // 分享成功
                                       [LZVideoPresenter saveShareCountWithVideoID:vModel.videoId ResultSuccess:^(NSInteger totleCount){
                                           vModel.shareCount = totleCount;
                                           if (refreshBlock) {
                                               refreshBlock(VideoCellRefreshType_Share,YES);
                                           }
                                       } ResultFailed:^(NSString *remind) {
                                           
                                       }];
                                   }
                               }];
    
    [self.shareView shareViewClose];
}

- (TShareView *)shareView {
    
    if (_shareView == nil) {
        _shareView = [[TShareView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    }
    
    return _shareView;
}

@end
